home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / lib_pcre / pcre / pcre_newline.c < prev    next >
Encoding:
C/C++ Source or Header  |  2007-11-20  |  6.0 KB  |  177 lines

  1. /*************************************************
  2. *      Perl-Compatible Regular Expressions       *
  3. *************************************************/
  4.  
  5. /* PCRE is a library of functions to support regular expressions whose syntax
  6. and semantics are as close as possible to those of the Perl 5 language.
  7.  
  8.                        Written by Philip Hazel
  9.            Copyright (c) 1997-2007 University of Cambridge
  10.  
  11. -----------------------------------------------------------------------------
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are met:
  14.  
  15.     * Redistributions of source code must retain the above copyright notice,
  16.       this list of conditions and the following disclaimer.
  17.  
  18.     * Redistributions in binary form must reproduce the above copyright
  19.       notice, this list of conditions and the following disclaimer in the
  20.       documentation and/or other materials provided with the distribution.
  21.  
  22.     * Neither the name of the University of Cambridge nor the names of its
  23.       contributors may be used to endorse or promote products derived from
  24.       this software without specific prior written permission.
  25.  
  26. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. POSSIBILITY OF SUCH DAMAGE.
  37. -----------------------------------------------------------------------------
  38. */
  39.  
  40.  
  41. /* This module contains internal functions for testing newlines when more than
  42. one kind of newline is to be recognized. When a newline is found, its length is
  43. returned. In principle, we could implement several newline "types", each
  44. referring to a different set of newline characters. At present, PCRE supports
  45. only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
  46. and NLTYPE_ANY. The full list of Unicode newline characters is taken from
  47. http://unicode.org/unicode/reports/tr18/. */
  48.  
  49.  
  50. #ifdef HAVE_CONFIG_H
  51. #include "config.h"
  52. #endif
  53.  
  54. #include "pcre_internal.h"
  55.  
  56.  
  57.  
  58. /*************************************************
  59. *      Check for newline at given position       *
  60. *************************************************/
  61.  
  62. /* It is guaranteed that the initial value of ptr is less than the end of the
  63. string that is being processed.
  64.  
  65. Arguments:
  66.   ptr          pointer to possible newline
  67.   type         the newline type
  68.   endptr       pointer to the end of the string
  69.   lenptr       where to return the length
  70.   utf8         TRUE if in utf8 mode
  71.  
  72. Returns:       TRUE or FALSE
  73. */
  74.  
  75. BOOL
  76. _pcre_is_newline(const uschar *ptr, int type, const uschar *endptr,
  77.   int *lenptr, BOOL utf8)
  78. {
  79. int c;
  80. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  81.     if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
  82. #else
  83.     c = *ptr; /* AutoHotkey. */
  84. #endif /* AutoHotkey. */
  85.  
  86. if (type == NLTYPE_ANYCRLF) switch(c)
  87.   {
  88.   case 0x000a: *lenptr = 1; return TRUE;             /* LF */
  89.   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
  90.                return TRUE;                          /* CR */
  91.   default: return FALSE;
  92.   }
  93.  
  94. /* NLTYPE_ANY */
  95.  
  96. else switch(c)
  97.   {
  98.   case 0x000a:                                       /* LF */
  99.   case 0x000b:                                       /* VT */
  100.   case 0x000c: *lenptr = 1; return TRUE;             /* FF */
  101.   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
  102.                return TRUE;                          /* CR */
  103. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  104.   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;   /* NEL */
  105. #else
  106.   case 0x0085: *lenptr = 1; return TRUE;             /* NEL */  /* AutoHotkey. */
  107. #endif /* AutoHotkey. */
  108.   case 0x2028:                                       /* LS */
  109.   case 0x2029: *lenptr = 3; return TRUE;             /* PS */
  110.   default: return FALSE;
  111.   }
  112. }
  113.  
  114.  
  115.  
  116. /*************************************************
  117. *     Check for newline at previous position     *
  118. *************************************************/
  119.  
  120. /* It is guaranteed that the initial value of ptr is greater than the start of
  121. the string that is being processed.
  122.  
  123. Arguments:
  124.   ptr          pointer to possible newline
  125.   type         the newline type
  126.   startptr     pointer to the start of the string
  127.   lenptr       where to return the length
  128.   utf8         TRUE if in utf8 mode
  129.  
  130. Returns:       TRUE or FALSE
  131. */
  132.  
  133. BOOL
  134. _pcre_was_newline(const uschar *ptr, int type, const uschar *startptr,
  135.   int *lenptr, BOOL utf8)
  136. {
  137. int c;
  138. ptr--;
  139. #ifdef SUPPORT_UTF8
  140. if (utf8)
  141.   {
  142.   BACKCHAR(ptr);
  143.   GETCHAR(c, ptr);
  144.   }
  145. else c = *ptr;
  146. #else   /* no UTF-8 support */
  147. c = *ptr;
  148. #endif  /* SUPPORT_UTF8 */
  149.  
  150. if (type == NLTYPE_ANYCRLF) switch(c)
  151.   {
  152.   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
  153.                return TRUE;                         /* LF */
  154.   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
  155.   default: return FALSE;
  156.   }
  157.  
  158. else switch(c)
  159.   {
  160.   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
  161.                return TRUE;                         /* LF */
  162.   case 0x000b:                                      /* VT */
  163.   case 0x000c:                                      /* FF */
  164.   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
  165. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  166.   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;  /* NEL */
  167. #else
  168.   case 0x0085: *lenptr = 1; return TRUE;            /* NEL */ /* AutoHotkey. */
  169. #endif /* AutoHotkey. */
  170.   case 0x2028:                                      /* LS */
  171.   case 0x2029: *lenptr = 3; return TRUE;            /* PS */
  172.   default: return FALSE;
  173.   }
  174. }
  175.  
  176. /* End of pcre_newline.c */
  177.